package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.common.enums.CommonState;
import com.yc.sole.erp.dto.SupplierDto;
import com.yc.sole.erp.entity.Supplier;
import com.yc.sole.erp.mapper.SupplierMapper;
import com.yc.sole.erp.request.SupplierQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 供应商表 服务接口实现
 *
 * @author yizuomin
 * @date 2023-05-07
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class SupplierRepository extends ServiceImpl<SupplierMapper, Supplier> {

    /**
     * 新增供应商表
     *
     * @param supplierDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveSupplier(SupplierDto supplierDto) {
        Supplier entity = new Supplier();
        BeanUtil.copyProperties(supplierDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改供应商表
     *
     * @param supplierDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateSupplierById(SupplierDto supplierDto) {
        Supplier entity = new Supplier();
        BeanUtil.copyProperties(supplierDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id删除供应商表
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteSupplierByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取供应商表
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public List<SupplierDto> selectSupplierByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), SupplierDto.class);
    }

    /**
     * 根据id获取供应商
     *
     * @param id
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public SupplierDto selectSupplierById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), SupplierDto.class);
    }

    /**
     * 根据供应商编号获取供应商
     *
     * @param supplierNo 供应商编号
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public SupplierDto selectSupplierBySupplierNo(String supplierNo) {
        LambdaQueryWrapper<Supplier> wrapper = Wrappers.<Supplier>lambdaQuery().eq(Supplier::getSupplierNo, supplierNo);
        return BeanUtil.copyProperties(this.getOne(wrapper), SupplierDto.class);
    }

    /**
     * 根据名称查询所有供应商
     *
     * @param supplierName
     * @return
     */
    public List<SupplierDto> selectSupplierByName(String supplierName) {
        LambdaQueryWrapper<Supplier> queryWrapper = Wrappers.<Supplier>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(supplierName), Supplier::getSupplierName, supplierName)
                .eq(Supplier::getState, CommonState.ENABLE)
                .orderByDesc(Supplier::getId);
        Page<Supplier> supplierPage = new Page<>(1, 10);
        return BeanUtil.copyToList(this.page(supplierPage, queryWrapper).getRecords(), SupplierDto.class);
    }

    /**
     * 根据名称查询所有供应商
     *
     * @param supplierNames
     * @return
     */
    public List<SupplierDto> selectSupplierByNames(Set<String> supplierNames) {
        LambdaQueryWrapper<Supplier> queryWrapper = Wrappers.<Supplier>lambdaQuery()
                .in(CollUtil.isNotEmpty(supplierNames), Supplier::getSupplierName, supplierNames);
        return BeanUtil.copyToList(this.list(queryWrapper), SupplierDto.class);
    }

    /**
     * 分页查询供应商表
     *
     * @param page
     * @return RestResult
     */
    public Paging<SupplierDto> selectSupplierPage(Paging page, SupplierQueryReq supplierPageReq) {
        LambdaQueryWrapper<Supplier> queryWrapper = Wrappers.<Supplier>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(supplierPageReq.getSupplierName()), Supplier::getSupplierName, supplierPageReq.getSupplierName())
                .like(CharSequenceUtil.isNotBlank(supplierPageReq.getPhoneNum()), Supplier::getPhoneNum, supplierPageReq.getPhoneNum())
                .eq(Objects.nonNull(supplierPageReq.getState()), Supplier::getState, supplierPageReq.getState())
                .orderByDesc(Supplier::getId);
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), SupplierDto.class);
    }

    /**
     * 查询所有供应商
     *
     * @return
     */
    public List<SupplierDto> selectAllSupplier(SupplierQueryReq queryReq) {
        LambdaQueryWrapper<Supplier> wrapper = Wrappers.<Supplier>lambdaQuery()
                .and(CharSequenceUtil.isNotBlank(queryReq.getKeyword()), w ->
                        w.like(Supplier::getSupplierName, queryReq.getKeyword())
                                .or().like(Supplier::getSupplierNo, queryReq.getKeyword()))
                .orderByAsc(Supplier::getFirstLetter);
        return BeanUtil.copyToList(this.list(wrapper), SupplierDto.class);
    }
}
